package cn.briarbear.myblog.controller.admin;

import cn.briarbear.myblog.entity.dto.AdminUserPwdModifyForm;
import cn.briarbear.myblog.entity.dto.IdListForm;
import cn.briarbear.myblog.entity.dto.ResponseStatus;
import cn.briarbear.myblog.enums.Message;
import cn.briarbear.myblog.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 管理员账户管理
 *
 * @auther Val1ant
 * @blog http://www.val1ant.xin
 * @create 2018-05-10-19:49
 */
@Controller
@RequestMapping("/admin_user")
public class AdminUserController {

    @Autowired
    private IAdminService adminService;
    /*
      * 后台用户管理页面
     */
    @GetMapping("/manage")
    public String adminUserManage() {
        return "admin/admin_user_manage";
    }

    /**
     * 后台用户管理 json 获取所有用户
     */
    @GetMapping("")
    @ResponseBody
    public Object adminUserList() {
        return adminService.getAdminUsers();
    }
    /**
     * 后台用户批量删除
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public Object jAdminUserDelete(@RequestBody IdListForm form) {
        adminService.deleteAdminUser(form);
        return new ResponseStatus(201,false);
    }

    /**
     * 后台用户密码修改页面
     */
    @GetMapping("/pwd_modify")
    public String adminUserPwdModify() {
        return "admin/admin_user_pwd_modify";
    }

    @PostMapping("/pwd_modify")
    public Object AdminUserPwdModify(@Valid AdminUserPwdModifyForm form, BindingResult bindingResult, HttpServletRequest request){

        if (bindingResult.hasErrors()) {
//            return responseSimpleError();
            return null;
        }

        String result = adminService.modifyUserPwd(form, request);

        if (result.equals(Message.MODIFY_SUCCESS)){
//            return responseSimpleOK();
            return null;
        }else {
//            return responseSimpleError();
            return null;
        }

    }

}
